package com.jp.knowledge.broad;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.jiayen.util.NetUtil;
import com.jiayen.util.ToasUtil;

/**
 * Created by jiayen on 16/12/27.
 *
 * 全局网络监听广播
 */

public class NetBroadCast extends BroadcastReceiver {

    private static boolean preState;
    private static boolean isFirst = true;

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean netState = NetUtil.isNetworkAvailable(context);
        if(!isFirst && preState == netState)
            return;
        if (netState) {
//            ToasUtil.toast(context, "连接到网络");
            LocalBroadcastManager.getInstance(context)
                    .sendBroadcast(new Intent(LocalBroadCast.CONNECT_NET_BROAD));
        } else {
            ToasUtil.toast(context, "网络已断开");
            LocalBroadcastManager.getInstance(context)
                    .sendBroadcast(new Intent(LocalBroadCast.UNCONNECT_NET_BROAD));
        }
        preState = netState;
        if(isFirst)
            isFirst = false;
    }
}
